<?php

namespace Console\Controller;

use Console\Common\Library\Page;

class MyTaskController extends CstBaseController
{
    private $taskLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->taskLogic = A('Console/Task', 'Logic');
    }

    public function taskList()
    {
        $typeDic = $this->taskLogic->getTypeDic();
        $this->assign('typeDic', $typeDic);
        $this->display();
    }

    public function addTask()
    {
        $typeDic = $this->taskLogic->getTypeDic();
        $this->assign('typeDic', $typeDic);
        $this->display();
    }

    public function editTask()
    {
        $id = I('get.id');
        if (empty($id)) {
            $this->sysErr('任务id不能为空');
        }

        $typeDic = $this->taskLogic->getTypeDic();

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $infoRes = $this->taskLogic->getInfo(array(
            'id' => $id,
            'user_id' => $userId,
        ));
        $info = $infoRes['Result'];
        if (empty($info)) {
            $this->sysErr('未找到该任务');
        }

        $this->assign('typeDic', $typeDic);
        $this->assign('info', $info);
        $this->display();
    }

    /**
     * 获取我发布的任务列表.
     */
    public function doGetMyTaskList()
    {
        $data = I('post.');
        $state = $data['state'];
        $page = $data['page'] ? $data['page'] : 1;
        $pageSize = 20;
        unset($data['page']);
        unset($data['state']);

        if ($state) {
            $now = date('Y-m-d H:i:s');
            switch ($state) {
                case 1:
                    $data['begin_time'] = array('EGT', $now);
                    break;
                case 2:
                    $data['begin_time'] = array('ELT', $now);
                    $data['end_time'] = array('EGT', $now);
                    break;
                case 3:
                    $data['end_time'] = array('ELT', $now);
                    break;
            }
        }

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];
        $data['user_id'] = $userId;

        $listRes = $this->taskLogic->getList($page, $pageSize, $data);
        $data = $listRes['Result'];
        $total = $data['total'];
        $list = $data['list'];

        $PageLib = new Page($total, $pageSize);
        $pageHtml = $PageLib->show();
        $pageTotal = ceil($total / $pageSize);

        $typeDic = $this->taskLogic->getTypeDic();

        $this->assign('typeDic', $typeDic);
        $this->assign('total', $total);
        $this->assign('pageTotal', $pageTotal);
        $this->assign('pageHtml', $pageHtml);
        $this->assign('list', $list);
        $this->assign('pageHtml', $pageHtml);
        $html = $this->fetch('doGetMyTaskList');

        $res = array(
            'list' => $list,
            'html' => $html,
            'total' => $total,
            'pageTotal' => $pageTotal,
        );
        $this->ajaxReturn(static::$res->suc('成功', $res));
    }

    /**
     * 添加一个任务
     */
    public function doAddTask()
    {
        $data = I('post.');

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];
        $phone = $userInfo['phone'];

        $res = $this->taskLogic->addTask($userId, $phone, $data);
        $this->ajaxReturn($res);
    }

    /**
     * 编辑一个任务
     */
    public function doEditTask()
    {
        $data = I('post.');

        $id = $data['id'];
        unset($data['id']);

        if (empty($id)) {
            $this->ajaxReturn(static::$res->err('任务ID不能为空'));
        }

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $infoRes = $this->taskLogic->getInfo(array(
            'id' => $id,
            'user_id' => $userId,
        ));
        $info = $infoRes['Result'];
        if (empty($info)) {
            $this->ajaxReturn(static::$res->err('任务未找到'));
        }

        $res = $this->taskLogic->editTask($userId, $id, $data);

        if(static::$res->isErr($res)){
            $this->ajaxReturn($res);
        }

        if (!empty($data['test_user']) && $data['test_user'] > $info['test_user']) {
            $joinLogic = A('Console/Join', 'Logic');
            $joinLogic->addTestUser($id, $data['test_user']);
        }

        $this->ajaxReturn($res);
    }

    /**
     * 删除任务
     */
    public function doDelTask()
    {
        $id = I('post.id');
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $infoRes = $this->taskLogic->getInfo(array(
            'id' => $id,
            'user_id' => $userId,
        ));
        $info = $infoRes['Result'];
        if (empty($info)) {
            $this->ajaxReturn(static::$res->err('任务未找到'));
        }

        $res = $this->taskLogic->delTask($userId, $id);
        $this->ajaxReturn($res);
    }
}
